<?php header('P3P: CP="CAO PSA OUR"');
/**
 * @file
 * Unregister a user.
 * 
 * Allows a user to unregister their Facebook account from their Untold
 * account. This is done by deleting the entry that associates their Untold ID
 * and Facebook ID from Untold's database.
 */
require_once("../utils/query.php"); // Functions for querying MySQL.

// Get the current user's Facebook ID.
session_start();
if (empty($_SESSION['facebook_id']))
	die("Not logged into facebook.");
$facebook_id = $_SESSION['facebook_id'];

$registered = false;

// Query if an entry exists with the user's Facebook ID.
$result = query_select_untold_id($facebook_id);
if ($result)
{
	$row = mysql_fetch_assoc($result);
	if ($row)
	{
		$_SESSION['untold_id'] = $row['drupaluid'];
		$registered = true;
	}
}
?>
<html>
	<head>
		<script type="text/javascript" src=<?php echo LIB_JQUERY; ?>></script>
		<script type="text/javascript">
			function onSubmit() {
				$.post(
					"../ajax/unregisterFacebookUser.php",
					{},
					function(data) {
						if (data.success) {
							$("#message").html("Unregistration successful.");
							$("#button").hide();
						} else {
							$("#message").html(data.message);
						}
					},
					'json'
				);
			}
		</script>
	</head>
	<body>
		<a href="testList.php">Back to list</a><br><br>
		<?php
		if($registered) {
		?>
		<div id="message">Unregister account?</div>
		<input id="button" type="submit" value="Unregister" onclick="onSubmit()" />
		<?php
		} else {
			echo "Not registered.";
		}
		?>
	</body>
</html>
